﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
using System.Data;

namespace HQWeb.Procurement
{
    public partial class ApplyList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindList();
            }
        }
        #region 绑定列表数据
        private void BindList()
        {
            string title = this.txttitle.Text.Trim();
            if (applytime.Value == "")
            {
                DateTime date = DateTime.Now;
                applytime.Value = date.AddMonths(-1).ToShortDateString().Replace('/', '-');
            }
            DateTime begintime = Convert.ToDateTime(applytime.Value);
            if (endtime.Value == "")
            {
                DateTime date = DateTime.Now;
                endtime.Value = date.ToShortDateString().Replace('/', '-');
            }
            DateTime atendtime = Convert.ToDateTime(endtime.Value).AddDays(1);
            string state = ddl_state.SelectedValue;
            DataSet Apply = new DataSet();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            Apply = bll.GetApplyList(title, state, begintime, atendtime);
            GridView1.DataSource = Apply.Tables[0];
            GridView1.DataBind();
           
        }
        #endregion


        protected string getSatte(string strType)
        {
            string str = "";
            switch (strType)
            {
                case "1": str = "待审批"; break;
                case "2": str = "审批未通过"; break;
                case "3": str = "审批通过"; break;
                default:
                    break;
            }
            return str;
        }

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                int newPageIndex = 0;
                if (-2 == e.NewPageIndex)
                { 
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }
                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }
                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                { 
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }
                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 查看删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("EditData"))  //查看
            {
                Response.Redirect("Edit.aspx?sign=1&ID=" + strID);
            }
            else if (strCommand.Equals("UpdateData"))
            {
                Response.Redirect("Edit.aspx?sign=2&ID=" + strID);
            }
            else if (strCommand.Equals("View"))
            {
                Response.Redirect("Scan.aspx?sign=3&ID=" + strID);
            }
            else if (strCommand.Equals("DelData"))  //删除
            {
                BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
                Model.Procurement.T_PUB_PURCHASE PurchaseModel = new Model.Procurement.T_PUB_PURCHASE();
                bll.Delete(strID);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除" + PurchaseModel.TITLE + "名称", DateTime.Now);
                BindList();
            }
        }

        #endregion
        //查询
        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }
        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strUrl = string.Format("Edit.aspx?sign=add&userid={0}", Session["USER_ID"].ToString());
            Response.Redirect(strUrl);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int index = e.Row.RowIndex;
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string strState = drv.Row["STATE"].ToString();
                LinkButton btn2 = (LinkButton)e.Row.Cells[5].FindControl("btnEdit");
                if (strState == "1")
                {
                    btn2.Text = "编辑";
                    btn2.CommandName = "EditData";
                }
                else if (strState == "2")
                {
                    btn2.Text = "编辑";
                    btn2.CommandName = "UpdateData";
                }
                else
                {
                    LinkButton btn1 = (LinkButton)e.Row.Cells[3].FindControl("btnDelete");
                    btn1.Enabled = false;
                    btn1.OnClientClick = "";
                    btn2.Text = "查看";
                    btn2.CommandName = "View";
                }
            }
        }
    }
}